<template>
  <div class="user">
    <!-- <el-row class="topw">
      <el-col :span="24">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>园区服务</el-breadcrumb-item>
          <el-breadcrumb-item>政策列表</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
    </el-row> -->
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="政策列表" name="first">
          <el-row style="padding:10px 0">
            <el-col :span="24">
              <!-- 给组件绑定原生事件的话，需要一个.native的修饰符 -->
              <el-input placeholder="请输入内容" class="search-input" v-model="title" @keydown.native.enter="initList">
                <el-button slot="append" icon="el-icon-search" @click="initList"></el-button>
              </el-input>
              <el-button type="success" plain @click="addDialogFormVisible=true">添加政策</el-button>
            </el-col>
          </el-row>
          <el-table class="margin-20" v-loading="loading" :data="InCoRegList" border style="width: 100%">
            <el-table-column type="index" width="50">
            </el-table-column>
            <el-table-column prop="title" label="政策名称">
            </el-table-column>
            <el-table-column prop="notification" label="政策介绍" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="unit" label="主办单位">
            </el-table-column>
            <el-table-column prop="address" label="主办地址">
            </el-table-column>
            <el-table-column prop="name" label="政策文件">
              <template slot-scope="scope" >
                <div style="color:#4F94CD; cursor: pointer;" @click="wendang(scope.row)">{{ scope.row.name }}</div>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="125">
              <template slot-scope="scope">
                <el-button size="mini" type="primary" plain icon="el-icon-edit" @click="showEditDialog(scope.row)">
                </el-button>
                <el-button size="mini" type="danger" plain icon="el-icon-delete" @click="showDeleteDialog(scope.row)">
                </el-button>
                <!-- <el-button size="mini" type="warning" plain icon="el-icon-check" @click="showGrantDialog(scope.row)">
                </el-button> -->
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="1"
              :page-sizes="[10, 20, 30, 40]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
          </div>
          <!-- 添加用户对话框 -->
          <el-dialog title="添加政策" :visible.sync="addDialogFormVisible" width="70%">
            <el-form :model="addForm" label-width="120px" :rules="rules" ref="addUserForm">
              <el-form-item label="政策名称:" prop="title" >
                <el-input v-model="addForm.title" auto-complete="off" ></el-input>
              </el-form-item>
              <el-form-item label="主办地址:" prop="address" >
                <el-input v-model="addForm.address" auto-complete="off" ></el-input>
              </el-form-item>
              <el-form-item label="主板单位:" prop="unit" >
                <el-input v-model="addForm.unit" auto-complete="off" ></el-input>
              </el-form-item>
              <el-form-item label="政策介绍:" prop="notification" >
                <el-input v-model="addForm.notification" auto-complete="off" ></el-input>
              </el-form-item>
              <el-form-item label="开始时间" >
                <el-date-picker
                  v-model="value2"
                  type="daterange"
                  align="right"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  :picker-options="pickerOptions">
                </el-date-picker>
              </el-form-item>
              <!-- <el-form-item label="活动文档名称:" prop="url" >
                <el-input v-model="addForm.url" auto-complete="off" ></el-input>
              </el-form-item> -->
              <el-form-item label="上传文档:" prop="targetName">
                <el-upload
                  class="avatar-uploader"
                  action="https://leather.ztazy.cn:3031/sys/file/file" 
                  :headers="headers"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
                  <!-- <img v-if="imageUrl" :src="imageUrl" class="avatar"> -->
                  <el-button slot="trigger">选取文件</el-button>
                  <div style="color:#4F94CD;display:inline-block;cursor:pointer;padding-left:10px" v-if="imageUrl" slot="tip" @click="myClick">{{addForm.name}}</div>
                  <div  style="display:inline-block" v-else slot="tip"></div>
                </el-upload>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="addDialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="addUserSubmit('addUserForm')">确 定</el-button>
            </div>
          </el-dialog>
          <!-- 编辑用户对话框 -->
          <el-dialog title="编辑政策列表" :visible.sync="editDialogFormVisible">
            <el-form :model="editForm" label-width="120px" :rules="rules" ref="editUserForm">
              <el-form-item label="政策名称" prop="title">
                <el-input v-model="editForm.title" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="主办地址" prop="address">
                <el-input v-model="editForm.address" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="主办单位" prop="unit">
                <el-input v-model="editForm.unit" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="政策介绍" prop="notification">
                <el-input v-model="editForm.notification" auto-complete="off"></el-input>
              </el-form-item>
              <el-form-item label="开始时间" >
                <el-date-picker
                  v-model="value1"
                  type="daterange"
                  align="right"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  :picker-options="pickerOptions">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="上传文档:" prop="targetName">
                <el-upload
                  class="avatar-uploader"
                  action="https://leather.ztazy.cn:3031/sys/file/file" 
                  :headers="headers"
                  :show-file-list="false"
                  :on-success="edithandleAvatarSuccess"
                  :before-upload="editbeforeAvatarUpload">
                  <!-- <img v-if="imageUrl" :src="imageUrl" class="avatar"> -->
                  <el-button slot="trigger">选取文件</el-button>
                  <div style="color:#4F94CD;display:inline-block;cursor:pointer;padding-left:10px" v-if="editForm.name" slot="tip" @click="myeditClick">{{editForm.name}}</div>
                  <div  style="display:inline-block" v-else slot="tip"></div>
                </el-upload>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="editDialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="editUserSubmit('editUserForm')">确 定</el-button>
            </div>
          </el-dialog>
      </el-tab-pane>
      <el-tab-pane label="政策申请列表" name="second">
        <el-table class="margin-20" v-loading="loading" :data="ParSerList" border style="width: 100%">
          <el-table-column type="index" width="50">
          </el-table-column>
          <el-table-column prop="policyTitle" label="政策名称">
          </el-table-column>
          <el-table-column prop="companyName" label="企业名称">
          </el-table-column>
          <el-table-column prop="name" label="企业负责人">
          </el-table-column>
          <el-table-column prop="phone" label="联系电话">
          </el-table-column>
          <el-table-column prop="createS" label="创建时间">
          </el-table-column>
          <el-table-column label="操作" width="70">
            <template slot-scope="scope">
              <!-- <el-button size="mini" type="primary" plain icon="el-icon-edit" @click="showEditDialog(scope.row)">
              </el-button> -->
              <el-button size="mini" type="danger" plain icon="el-icon-delete" @click="showDeleteDialog(scope.row)">
              </el-button>
              <!-- <el-button size="mini" type="warning" plain icon="el-icon-check" @click="showGrantDialog(scope.row)">
              </el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <div class="page">
          <el-pagination @size-change="ParSerhandleSizeChange" @current-change="ParSerhandleCurrentChange" :current-page="1"
            :page-sizes="[10, 20, 30, 40]" :page-size="ParSerpagesize" layout="total, sizes, prev, pager, next, jumper" :total="ParSerListtotal">
          </el-pagination>
        </div>
      </el-tab-pane>
    </el-tabs>
    
  </div>
</template>
<script>
  import {
    ParSerlistList,
    ParSerList,
    addParSerlist,
    editParSerlist,
    deleteParSerlist
  } from '@/api'
  export default {
    data() {
      return {
        value1:"",
        value2:"",
        activeName: 'first',
        pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
          msg: '',
        myConfig: {
          autoHeightEnabled: false,
          initialFrameHeight: 300,
          initialFrameWidth: '100%',
          UEDITOR_HOME_URL: '/static/UEditor//',
          serverUrl: 'http://35.201.165.105:8000/controller.php'
        },
        InCoRegList: [],
        ParSerList: [],
        title:"",
        query: '',
        total: 0,
        ParSerListtotal:0,
        imageUrl:'',
        pagesize: 10,
        pagenum: 1,
        ParSerpagesize: 10,
        ParSerpagenum: 1,
        addDialogFormVisible: false,
        addForm: {
          title: '',
          rank: '',
          unit: '',
          endTime:'',
          startTime:'',
          name:'',
          targetName:'',
          notification:'',
          address:''
        },
        editDialogFormVisible: false,
        editForm: {
          title: '',
          rank: '',
          unit: '',
          endTime:'',
          startTime:'',
          name:'',
          targetName:'',
          policyId:'',
          notification:'',
          address:''
        },
        grantDialogFormVisible: false,
        grantForm: {},
        roleList: [],
        roleId: '',
        // 添加用户的表单验证
        rules: {
          username: [{
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }],
        }
      }
    },
    created() {
      this.initList()
    },
    computed: {
      headers() {
        return{
          "authorization": localStorage.getItem('mytoken') // 直接从本地获取token就行
        }
      }
    },
    methods: {
      handleClick(tab, event) {
      },
      dateChangebirthday1(val){
      console.log(val)
      this.addForm.startTime = val
      },
      dateChangebirthday2(val){
      console.log(val)
      this.addForm.endTime = val
      },
      editdateChangebirthday1(val){
      console.log(val)
      this.editForm.startTime = val
      },
      editdateChangebirthday2(val){
      console.log(val)
      this.editForm.endTime = val
      },
      edithandleAvatarSuccess(res, file) {
        console.log(res)
        console.log(file)
        this.imageUrl = URL.createObjectURL(file.raw);
        this.editForm.targetName = res.data.targetName
        console.log(this.editForm.targetName)
      },
      editbeforeAvatarUpload(file) {
        console.log(file)
        this.editForm.name = file.name
        // const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        // if (!isJPG) {
        //   this.$message.error('上传头像图片只能是 JPG 格式!');
        // }
        if (!isLt2M) {
          this.$message.error('上传头像文件大小不能超过 2MB!');
        }
        return isLt2M;
      },
      handleAvatarSuccess(res, file) {
        console.log(res)
        console.log(file)
        this.imageUrl = URL.createObjectURL(file.raw);
        this.addForm.targetName = res.data.targetName
        console.log(this.addForm.targetName)
      },
      beforeAvatarUpload(file) {
        console.log(file)
        this.addForm.name = file.name
        // const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        // if (!isJPG) {
        //   this.$message.error('上传头像图片只能是 JPG 格式!');
        // }
        if (!isLt2M) {
          this.$message.error('上传头像文件大小不能超过 2MB!');
        }
        return isLt2M;
      },
      myeditClick(){
        window.location.href = 'https://wisdom-city.oss-cn-hangzhou.aliyuncs.com/'+this.editForm.targetName
      },
      wendang(row){
        window.location.href = 'https://wisdom-city.oss-cn-hangzhou.aliyuncs.com/'+row.targetName
      },
      myClick(row){
        window.location.href = 'https://wisdom-city.oss-cn-hangzhou.aliyuncs.com/'+this.editForm.targetName
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.pagesize = val
        this.initList()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.pagenum = val
        this.initList()
      },
      ParSerhandleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.ParSerpagesize = val
        this.initList()
      },
      ParSerhandleCurrentChange(val) {
        console.log(`当前页: ${val}`)
        this.ParSerpagenum = val
        this.initList()
      },
      initList() {
        this.loading = true
        ParSerlistList({
          params: {
            title:this.title,
            page: this.pagenum-1,
            size : this.pagesize,
            sortField: 'createTime',
            sortType:'dsc'
          }
        }).then(res => {
          console.log(res)
          this.InCoRegList = res.data.content
          this.total = res.data.totalElements
        })
        ParSerList({
          params: {
            companyName:this.companyName,
            page: this.ParSerpagenum-1,
            size : this.ParSerpagesize,
            sortField: 'createTime',
            sortType:'dsc'
          }
        }).then(res => {
          console.log(res)
          this.ParSerList = res.data.content
          this.ParSerListtotal = res.data.totalElements
          this.loading = false
        })
      },
      // 改变用户状态
      changeUserState(row) {
        console.log(row)
        changeUserState({
          uid: row.id,
          type: row.mg_state
        }).then(res => {
          if (res.meta.status === 200) {
            this.$message({
              type: 'success',
              message: '修改用户状态成功'
            })
          } else {
            this.$message({
              type: 'warning',
              message: res.meta.msg
            })
          }
        })
      },
      closeDiaglog(){
        this.$refs['addUserForm'].resetFields()
      },
      // 添加用户
      addUserSubmit(formName) {
        var start = this.value2[0].split("-");
        var end = this.value2[1].split("-");
        var rank = start[1]+'月'+start[2]+'日至'+end[1]+'月'+end[2]+'日'
        this.addForm.rank = rank
        this.addForm.startTime = this.value2[0]
        this.addForm.endTime =  this.value2[1]
        console.log(this.addForm)
         this.$refs[formName].validate(valide => {
          if (valide) {
            console.log(123213)
            console.log(this.addForm)
            addParSerlist(this.addForm).then(res => {
              console.log(res)
              if (res.status === 0) {
                this.$message({
                  type: 'success',
                  message: '创建用户成功'
                })
              this.initList()
              } else {
                this.$message({
                  type: 'warning',
                  message: res.message
                })
              }
              this.addDialogFormVisible = false
            })
          }
        })
      },
      // 显示编辑用户对话框
      showEditDialog(row) {
        console.log(row)
        this.value1=[]
        this.editDialogFormVisible = true
            this.editForm.title = row.title
            this.editForm.rank = row.rank
            this.editForm.unit = row.unit
            this.editForm.startTime = row.startTime
            this.editForm.notification = row.notification
            this.editForm.address = row.address
            this.editForm.endTime = row.endTime
            this.editForm.policyId = row.policyId
            this.editForm.name = row.name
            this.editForm.targetName = row.targetName
            this.value1.push(row.startTime,row.endTime)
      },
      // 编辑用户提交
      editUserSubmit(formName) {
        console.log(this.value1)
        var start = this.value1[0].split("-");
        var end = this.value1[1].split("-");
        var rank = start[1]+'月'+start[2]+'日至'+end[1]+'月'+end[2]+'日'
        this.editForm.rank = rank
        this.editForm.startTime = this.value1[0]
        this.editForm.endTime =  this.value1[1]
        this.$refs[formName].validate(valide => {
          if (valide) {
            console.log(this.editForm)
            editParSerlist(this.editForm).then(res => {
              console.log(res)
              if (res.status === 0) {
                this.$message({
                  type: 'success',
                  message: '编辑成功'
                })
                this.editDialogFormVisible = false
                this.initList()
              } else {
                this.$message({
                  type: 'warning',
                  message: res.meta.msg
                })
              }
            })
          }
        })
      },
      // 显示删除对话框
      showDeleteDialog(row) {
        console.log(row)
        this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 执行删除用户操作
          deleteParSerlist({
          params: {
            policyId:row.policyId
          }
        }).then(res => {
            if (res.status === 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.initList()
            } else {
              this.$message({
                type: 'warning',
                message: res.message
              })
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      // 显示分配角色对话框
      // showGrantDialog(row) {
      //   this.grantForm = row
      //   this.grantDialogFormVisible = true
      //   getRoleList().then(res => {
      //     console.log(res)
      //     if (res.meta.status === 200) {
      //       this.roleList = res.data
      //     }
      //   })
      // },
      // 分配角色
      // grantUserSubmit() {
      //   if (!this.roleId) {
      //     this.$message({
      //       type: 'warning',
      //       message: '角色不能为空，请选择！'
      //     })
      //   } else {
      //     grantUserRole({
      //       id: this.grantForm.id,
      //       rid: this.roleId
      //     }).then(res => {
      //       if (res.meta.status === 200) {
      //         this.$message({
      //           type: 'success',
      //           message: '设置角色成功'
      //         })
      //         this.grantDialogFormVisible = false
      //       } else {
      //         this.$message({
      //           type: 'error',
      //           message: res.meta.msg
      //         })
      //       }
      //     })
      //   }
      // }
    }
  }

</script>
<style lang="scss" scoped>
  .user {
    .margin-20 {
      margin: 20px 0;
    }

    .search-input {
      width: 300px;
    }

    .page {
      padding: 5px 0;
      background-color: #D3DCE6;
    }
  }

</style>
